﻿namespace MEF_Test.Bricks.MediaBrowser
{
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;

    using Microsoft.Practices.Prism.Commands;

    public class BrowserSelectedNodeChangedBehavior : CommandBehaviorBase<Selector>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="BrowserSelectedNodeChangedBehavior"/> class.
        /// </summary>
        /// <param name="control">The control.</param>
        public BrowserSelectedNodeChangedBehavior(Selector control)
            : base(control)
        {
            control.SelectionChanged += OnSelectedItemChanged;
        }

        /// <summary>
        /// Called when selected item changes.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Controls.SelectionChangedEventArgs"/> instance containing the event data.</param>
        private void OnSelectedItemChanged(object sender, SelectionChangedEventArgs e)
        {
            // only notify a selection change when an item is actually selected.
            if (e.RemovedItems.Count <= e.AddedItems.Count)
            {
                ExecuteCommand();  
            }
        }
    }
}